<template>
  <div>
    <el-dialog title="新增留言" :visible.sync="dialogFormVisible">
      <el-form :model="formData" status-icon>
        <el-form-item label="你需要留言的内容：" label-width="150px">
          <el-input v-model="formData.activityContent" type="textarea" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addHandle">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const { getUserCode } = require('@/utils/auth')
const { addUserActiviInfoData, modifyUserActiviInfoData } = require('@/api/useractivityInfo')
export default {
  name: 'ActivityModal',
  data() {
    return {
      formType: 'add',
      dialogFormVisible: false, // 是否展示对话框
      formData: {
        activityUserCode: getUserCode(),
        activityCode: '',
        activityContent: ''
      } // 表单数据
    }
  },
  methods: {
    showDialog(obj, type) {
      this.formType = type // 赋值表单模式 新增模式还是修改模式
      if (type === 'add') { // 滞空
        this.formData.activityContent = ''
        console.log(this.formData)
        this.dialogFormVisible = !this.dialogFormVisible
        return
      }
      this.formData.activityContent = obj.activityContent // 将父组件传来的留言板内容保存
      this.formData.activityCode = obj.activityCode // 将父组件传来的留言板主键保存
      this.dialogFormVisible = !this.dialogFormVisible
    },
    addHandle() {
      console.log('你点击了提交按钮')
      if (this.formData.activityContent.length === 0) {
        this.$message({
          type: 'error',
          iconClass: 'el-icon-loading',
          message: '留言内容不能为空！',
          duration: 3 * 1000
        })
        return
      }
      if (this.formType === 'add') {
        this.postActivityData()
      } else {
        this.postModifyActivityData()
      }
    },
    async postActivityData() {
      const res = await addUserActiviInfoData(this.formData)
      const header = res.data.respOutMsgHeader
      if (header.respCode === 200) {
        this.$message({
          type: 'success',
          iconClass: 'el-icon-loading',
          message: '用户留言成功!',
          duration: 1.5 * 1000
        })
        this.$emit('refreshDataMethod')
      } else {
        this.$message({
          type: 'error',
          iconClass: 'el-icon-loading',
          message: '留言失败，错误：' + header.msg,
          duration: 3 * 1000
        })
      }
      this.dialogFormVisible = false
    },
    modifyHandle() {
      this.postModifyActivityData()
    },
    async postModifyActivityData() {
      const res = await modifyUserActiviInfoData(this.formData)
      const header = res.data.respOutMsgHeader
      if (header.respCode === 200) {
        this.$message({
          type: 'success',
          iconClass: 'el-icon-loading',
          message: '用户留言修改成功!',
          duration: 1.5 * 1000
        })
        this.$emit('refreshDataMethod')
      } else {
        this.$message({
          type: 'error',
          iconClass: 'el-icon-loading',
          message: '留言失败，错误：' + header.msg,
          duration: 3 * 1000
        })
      }
      this.dialogFormVisible = false
    }
  }
}
</script>

<style scoped>

</style>
